
#include "net.h"

//**********************************************************

#define MAX_BUFF_SIZE  (12*1024*1024)

#define LISTEN_PORT    (8888)

//**********************************************************

static pthread_t  		  net_s_thread;
static int 				  net_s_sockfd = -1;
static int 				  net_s_connfd = -1; 
static int 				  net_s_len = 0;

static struct sockaddr_in net_s_client;

static char 			  net_s_buff[MAX_BUFF_SIZE];
//**********************************************************
int net_server_start(void)
{
    int n;
    struct sockaddr_in server;

    net_s_sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(net_s_sockfd < 0){
        log_e("socket creation failed...\n");
        return -1;
    }

    log_i("socket successfully created..\n");

	bzero(&server, sizeof(server));

    server.sin_family      = AF_INET;
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    server.sin_port        = htons(LISTEN_PORT);

    if((bind(net_s_sockfd, (struct sockaddr*)&server, sizeof(server))) != 0) {
        log_e("socket bind failed...\n");
        return -1;
    }

    log_i("socket successfully binded..\n");
	
    if((listen(net_s_sockfd, 5)) != 0){
        log_e("Listen failed...\n");
        return -1;
    }



	return 0;
}


void* net_server_task(void *arg)
{
	int ret = -1;

	while(1)
	{
		log_i("server listening...\n");
		
		net_s_len    = sizeof(net_s_client);
		net_s_connfd = accept(net_s_sockfd, (struct sockaddr*)&net_s_client, &net_s_len);
		if(net_s_connfd < 0){
			log_e("server acccept failed...\n");
			return -1;
		}
		
		log_i("server acccept the client...\n");

		while(1)
		{
			usleep(1000);
			//*******************************************************************
			bzero(net_s_buff, MAX_BUFF_SIZE);
			//*******************************************************************
			ret = read(net_s_connfd, net_s_buff, sizeof(net_s_buff));
			if(ret <= 0){
				log_e("read error\n");
				break;
			}

			ret = send(net_s_connfd, net_s_buff, strlen(net_s_buff), 0);
			if(ret <= 0){
				log_e("send error\n");
				break;
			}
			//*******************************************************************
			log_i("from client: %s\n", net_s_buff);
			//*******************************************************************
			if(!strcmp(net_s_buff,"up"))
			{
				message_packet_send(MSG_KEY_UP,0,0);
				continue;
			}
			if(!strcmp(net_s_buff,"down"))
			{
				message_packet_send(MSG_KEY_DOWN,0,0);
				continue;
			}
			if(!strcmp(net_s_buff,"left"))
			{
				message_packet_send(MSG_KEY_LEFT,0,0);
				continue;
			}
			if(!strcmp(net_s_buff,"right"))
			{
				message_packet_send(MSG_KEY_RIGHT,0,0);
				continue;
			}
			if(!strcmp(net_s_buff,"prev"))
			{
				message_packet_send(MSG_KEY_PREV,0,0);
				continue;
			}
			if(!strcmp(net_s_buff,"next"))
			{
				message_packet_send(MSG_KEY_NEXT,0,0);
				continue;
			}
			//*******************************************************************
		}

		usleep(1000);
    }
}


int net_server_init(void)
{
	int ret = -1;

	net_server_start();

	ret=pthread_create(&net_s_thread, NULL, net_server_task, NULL);
	if(ret != 0){
		log_e("thread error=%s\n",strerror(ret));
	}


	return 0;
}


int net_server_deinit(void)
{
	int ret = -1;

	close(net_s_connfd);
	net_s_connfd = -1;
	


	return 0;
}





